Skip to content

fix: set redirected property on redirect responses#746

Merged
kettanaito merged 1 commit intomswjs:mainfrom
erikshestopal:fetch-redirected
Sep 25, 2025
Merged

fix: set redirected property on redirect responses#746
kettanaito merged 1 commit intomswjs:mainfrom
erikshestopal:fetch-redirected

Conversation

@erikshestopal
Copy link
Copy Markdown
Contributor

Description

Sets the .redirected property on redirected requests to comply with the Fetch spec.

Failing scenario using msw:

it.only('sets the .redirected property', async () => {
  server.use(
    http.get(
      'https://fakesite.org/redirect/10',
      () => new HttpResponse(null, { status: 301, headers: { location: '/redirect/11' }, })
    ),
    http.get('https://fakesite.org/redirect/11', () => HttpResponse.json({ redirected: true }))
  );
  const response = await fetch('https://fakesite.org/redirect/10', { method: 'GET' });
  const json = await response.json();
  expect(json).toEqual({ redirected: true });
  expect(response.redirected).toBe(true);
});

@kettanaito kettanaito changed the title fix: set redirected property on intercepted fetch requests fix: set redirected property on redirect responses Sep 18, 2025
*/

requestInit.headers = request.headers
return fetch(new Request(locationUrl, requestInit))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting that fetch doesn't set that property based on the location header. I suppose the reason is because it's the end response that's marked as redirected?

@erikshestopal did you by any chance have a reference to the spec that describes .redirected?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going off the undici implementation which sets the url list but for mockiing I opted for a simpler approach.

https://github.com/search?q=repo%3Anodejs%2Fundici+redirected&type=code

Copy link
Copy Markdown
Member

@kettanaito kettanaito left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing this, @erikshestopal!

@kettanaito kettanaito merged commit f582983 into mswjs:main Sep 25, 2025
2 checks passed
@kettanaito
Copy link
Copy Markdown
Member

Released: v0.39.7 🎉

This has been released in v0.39.7!

Make sure to always update to the latest version (npm i @mswjs/interceptors@latest) to get the newest features and bug fixes.


Predictable release automation by @ossjs/release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants